package com.gary.lockDemo;

/**
 * -Xint 禁用jit热点代码优化
 * @author peng_rui
 * @date 2022/7/18
 */
public class VolatileTested {
    static volatile boolean stop = false;

    public static void main(String[] args) {
        System.out.println("stop start = " + stop);
        new Thread(() -> {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            stop = true;
            System.out.println(" modify stop = true");
        }).start();

        new Thread(() -> {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            System.out.println(" read stop = " + stop);
        }).start();

        foo();

    }
    static void foo() {
        System.out.println("foo star ...");
        int i = 0;
        while (!stop) {
            i++;
        }
        System.out.println("stopped .... " + i);
    }
}
